package com.emailsystem.emailsystem.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.emailsystem.emailsystem.dao.Impl.EmailDAO;
import com.emailsystem.emailsystem.dao.Impl.UserDAO;
import com.emailsystem.emailsystem.domain.Completion;
import com.emailsystem.emailsystem.domain.Connection;
import com.emailsystem.emailsystem.domain.Email;
import com.emailsystem.emailsystem.service.ClientService;
import com.emailsystem.emailsystem.service.impl.client.Client_send;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

@Service
@Data
public class ClientServiceImpl implements ClientService {

    @Autowired
    private  POP3Impl pop3;
    @Autowired
    private IMAPImpl imap;
    private static Socket socket;
    public static boolean connection_state = false;
    @Autowired
    private EmailDAO emailDAO;
    @Autowired
    private UserDAO userDao;

    public static Connection connection;


    //
    public ArrayList<Email> getDeleted(String account){
        return emailDAO.selectDeleted(account);
    }

    //返回新增了多少封
    public Integer DBvsCurrent(Integer DB_nums, String account, String password) throws IOException {
        pop3.init("pop.qq.com",995);
        return pop3.getCurrentNums(account,password)-DB_nums;
    }

    public Integer DBvsCurrentInIMAP(Integer DB_nums,String boxType, String account, String password) throws IOException {
        imap.init("imap.qq.com", 993);
        int numsInRemote=imap.getCurrentNums(boxType, account, password);
        //此文件夹不存在
        if(numsInRemote==-1){
            return -1;
        }
        return numsInRemote - DB_nums;
    }

    public void receiveEmails(String protocol, String boxType, String account, String password, int begin, int length) throws IOException {
        if ("POP3".equals(protocol)) {
            pop3.receiverNewEmail(account, password,begin,length);
        } else {
            imap.receiveEmail(boxType, account, password,begin,length);
        }
    }

    @Override
    public void connect(Connection connection, JSONObject object) {
        try {
            connection = connection;
            socket = new Socket(connection.getIP(), connection.getPort());
            connection_state = true;
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            //new Thread(new Client_send(socket, oos, object)).start();
        } catch (Exception e) {
            e.printStackTrace();
            connection_state = false;
        }
    }

    @Override
    public String connect(String account, String password) throws IOException {
        pop3.init("pop.qq.com",110);
        return pop3.login(account,password);
    }

    public Integer getEmailNumsInIMAP(String account,String type){
        Completion completion = userDao.getCompletionByAccount(account);
        if (null==completion) {
            userDao.insertCompletionInIMAP(account,"","","","","");
            completion = userDao.getCompletionByAccount(account);
        }
        String Emails="";
        switch (type){
            case "inbox":
                Emails= completion.getReceiveEmail();
                break;
            case "drafts":
                Emails=completion.getDraftEmail();
                break;
            case "favourite":
                Emails=completion.getFavouriteEmail();
                break;
            case "sent":
                Emails=completion.getSentEmail();
                break;
            case "rubbish":
                Emails = completion.getRubbishEmail();
        }
        if (null==Emails||Emails.isEmpty()) {
            return 0;
        }
        return Emails.split(",").length;
    }

    public Integer getEmailNumsInPOP3(String account, Character type) {
        Completion completion = userDao.getCompletionByAccount(account);
        if (null==completion) {
            userDao.insertCompletionInPOP3(account,"");
            completion = userDao.getCompletionByAccount(account);
        }
        switch (type){
            case 'r':
                String receiveEmail = completion.getReceiveEmail();

                if (null==receiveEmail) {
                    return 0;
                }
                return receiveEmail.split(",").length;
        }
        return 0;
    }

    @Override
    public ArrayList<Email> getAllEmails(String account,String boxType) {
        Completion completion = userDao.getCompletionByAccount(account);
        String Emails = "";
        switch (boxType) {
            case "inbox" -> Emails = completion.getReceiveEmail();
            case "drafts" -> Emails = completion.getDraftEmail();
            case "favourite" -> Emails = completion.getFavouriteEmail();
            case "sent" -> Emails = completion.getSentEmail();
            case "rubbish" -> Emails = completion.getRubbishEmail();
        }
        ArrayList<Email> emails = emailDAO.selectAllByEmailID(Emails.split(","));
        return emails;
    }

    //更新email只修改本地，不更新远程，因为不清楚这是第几封邮件
    @Override
    public void updateEmail(String emailID, String type,Boolean state) {

        switch (type) {
            case "seen"->emailDAO.updateSeen(emailID,state);
            case "flagged"->emailDAO.updateFlagged(emailID,state);
            case "deleted"->emailDAO.updateDeleted(emailID,state);
        }
    }
    public static void connect(Connection connection, JSONObject object, int num) {
        try {
            socket = new Socket(connection.getIP(), connection.getPort());
            connection_state = true;
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            //new Thread(new Client_send(socket, oos, object)).start();
        } catch (Exception e) {
            e.printStackTrace();
            connection_state = false;
        }
    }
}
